package com.leather.activity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.activity.config.MQConfig;
import com.leather.activity.feignclient.UserClient;
import com.leather.activity.mapper.ActivityGetMapper;
import com.leather.activity.mapper.ActivityMapper;
import com.leather.activity.service.ActivityService;
import com.leather.activity.vo.ActivityAddVo;
import com.leather.common.entity.Activity;
import com.leather.common.entity.ActivityGet;
import com.leather.common.entity.User;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.BeanKit;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.toolkits.DateKit;
import com.leather.common.vo.MessageVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ActivityServiceImpl  extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserClient userClient;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private ActivityGetMapper activityGetMapper;

    @Override
    public Result index(int pageId) {
        return Result.success(activityMapper.index(pageId));
    }

    @Override
    public Result userIndex(int userId) {
        return Result.success(activityMapper.userIndex(userId));
    }

    @Override
    public Result addActivity(ActivityAddVo activityAddVo) {
        Result result = activityAddVo.validate();
        if(!result.isSuccess()) {
            return result;
        }
        Activity activity = (Activity) result.getData();
        CheckKit.check(save(activity),CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result userActivity(int userId) {
        List<Activity> lists = query().eq("user_id", userId).list();
        return Result.success(lists);
    }

    @Override
    public Result deleteActivity(int activityId, int userId) {
        int count = activityMapper.deleteActivity(activityId,userId);
        CheckKit.check(count>0, CodeMsg.IS_PASS_IN);
        return Result.success("ok");
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Result getIn(int activityId, int userId) {
        CheckKit.check(activityId>0,CodeMsg.ERROR_ACTIVITY_ID);
        CheckKit.check(userId>0,CodeMsg.ERROR_USER_ID);
        User user = userClient.getUserById(userId);
        CheckKit.check(user!=null,CodeMsg.ERROR_USER_ID);
        Activity activity = activityMapper.getActivityById(activityId);
        User fabuUser = userClient.getUserById(activity.getUserId());
        if(activity.getNowPeople()==activity.getMaxPeople()){
            return Result.error(CodeMsg.HAS_FIXED);
        }
        int count = activityMapper.updateActivity(activityId);
        CheckKit.check(count>0,CodeMsg.HAS_FIXED);
        ActivityGet activityGet = new ActivityGet();
        activityGet.setId(0);
        activityGet.setUserId(userId);
        activityGet.setActivityId(activityId);
        activityGet.setCreateTime(DateKit.now());
        count = activityGetMapper.insert(activityGet);
        CheckKit.check(count>0,CodeMsg.SERVER_ERROR);
        String info = user.getRealName() + "-"  + user.getPhone();
        MessageVo messageVo = new MessageVo();
        messageVo.setMessageType(0);
        messageVo.setMessageTitle("[活动报名通知]");
        messageVo.setMessageContent("你发布的活动已有人报名,领取人信息:" + info);
        messageVo.setMessageUrl("/pages/activity/useractivity");
        messageVo.setUserId(activity.getUserId());
        //发送消息提醒
        amqpTemplate.convertAndSend(MQConfig.MESSAGE_QUENE, BeanKit.beanToString(messageVo));
        String message = fabuUser.getPhone() + "-" + info;
        //发送短信
        amqpTemplate.convertAndSend(MQConfig.ACTIVITY_MOBOLE_QUENE, message);
        MessageVo messageVo1 = new MessageVo();
        messageVo1.setMessageType(0);
        messageVo1.setMessageTitle("[活动报名通知]");
        messageVo1.setMessageContent("你已成功报名活动"+activity.getActivityTitle());
        messageVo1.setMessageUrl("/pages/activity/myactivity");
        messageVo1.setUserId(userId);
        amqpTemplate.convertAndSend(MQConfig.MESSAGE_QUENE, BeanKit.beanToString(messageVo1));
        return Result.success("ok");
    }

    @Override
    public Result getPassedList(int activityId) {
        CheckKit.check(activityId>0,CodeMsg.ERROR_ACTIVITY_ID);
        return Result.success(activityMapper.getPassedUser(activityId));
    }
}
